
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;
import com.kugou.game.sdk.utils.RSAUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:密保问题取回密码
 *
 * @author wcj
 * @since 2015年8月19日 下午3:57:13
 */
public class GetPasswordByProblemTask {

    public GetPasswordByProblemResponse request(String username, String password, String unixTime,
            String token) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(username);
        GetPasswordByProblemResponse respObj = new GetPasswordByProblemResponse();
        ResponsePackage resp = new GetPasswordByProblemResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_GET_PASSWD_BY_QUESTION);
        params.put("username", username);
        params.put("password", RSAUtil.getRASEncrypt(UrlEncodeUtil.encode(password, "utf-8")));
        params.put("unixtime", unixTime);
        params.put("flowtoken", token);
        requestParams.add(params);

        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    static class GetPasswordByProblemResponsePackage extends
            HttpBaseResponsePackage<GetPasswordByProblemResponse> {

        @Override
        protected void handleResponse(GetPasswordByProblemResponse resetPasswordResponse,
                String jsonStr) {
            try {
                LogUtil.d("response", "GetPasswordByProblemTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {

                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    resetPasswordResponse.setCode(code);
                    resetPasswordResponse.setMessage(message);
                    resetPasswordResponse.setPrompt(prompt);

                    if (resetPasswordResponse.isUseful()
                            && cmd.equals(ProtocolCmd.CMD_GET_PASSWD_BY_QUESTION)) {
                        JSONObject data = jsonResp.optJSONObject("data");
                        String openid = data.getString("kopenid");
                        String kugouToken = data.getString("kgtoken");
                        // 用于查找本地是否有该openid命名的记录文件
                        resetPasswordResponse.setOpenid(openid);
                        resetPasswordResponse.setKugouToken(kugouToken);
                    }
                }

            } catch (Exception e) {
            }
        }
    }

    public static class GetPasswordByProblemResponse extends ResponseMessage {

        String openid;

        String kugouToken;

        public String getOpenid() {
            return openid;
        }

        public void setOpenid(String openid) {
            this.openid = openid;
        }

        public String getKugouToken() {
            return kugouToken;
        }

        public void setKugouToken(String kugouToken) {
            this.kugouToken = kugouToken;
        }
    }
}
